package com.xinjie.www.dao.post;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.po.Post;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PostDaoImpl implements PostDao {
    //写帖子
    @Override
    public int writePost(Connection connection, String title, String tieba, String time, String content, String writer) throws SQLException {
        PreparedStatement pstm = null;
        int excute = 0;
        if (connection != null) {
            String sql = "insert into post (title,tieba,time,content,writer,comment,thumb)values(?,?,?,?,?,?,?)";
            Object params[] = {title, tieba, time, content, writer, 0, 0};
            excute = BaseDao.excute(connection, pstm, sql, params);
            if (excute > 0) {
                String sql2 = "update tieba set post_num=post_num+1 where name=?";
                Object[] params2 = {tieba};
                int excute2 = BaseDao.excute(connection, pstm, sql2, params2);
            }
            BaseDao.closeResource(null, pstm, null);
        }
        return excute;
    }

    //查看自己发布的贴子
    @Override
    public List<Post> getOwnPost(Connection connection, String writer) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Post> posts = new ArrayList<>();
        String sql = "select * from post where writer = ?";
        Object[] params = {writer};
        pstm = connection.prepareStatement(sql);
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            Post post = new Post();
            post.setPostId(rs.getInt("post_id"));
            post.setTitle(rs.getString("title"));
            post.setTieba(rs.getString("tieba"));
            post.setTime(rs.getString("time"));
            post.setContent(rs.getString("content"));
            post.setComment(rs.getInt("comment"));
            post.setThumb(rs.getInt("thumb"));
            posts.add(post);
        }
        BaseDao.closeResource(null, pstm, rs);
        return posts;
    }

    //管理员查看管理贴吧下的帖子
    @Override
    public List<Post> getAllPost(Connection connection, String managerName) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Post> posts = new ArrayList<>();
        String sql = "select p.* from post p ,tieba t where t.manager=? and p.tieba=t.name";
        Object[] params = {managerName};
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            Post post = new Post();
            post.setPostId(rs.getInt("post_id"));
            post.setTitle(rs.getString("title"));
            post.setTieba(rs.getString("tieba"));
            post.setTime(rs.getString("time"));
            post.setContent(rs.getString("content"));
            post.setWriter(rs.getString("writer"));
            post.setComment(rs.getInt("comment"));
            post.setThumb(rs.getInt("thumb"));
            posts.add(post);
        }
        BaseDao.closeResource(null, pstm, rs);
        return posts;
    }

    //删除帖子
    @Override
    public int deletePost(Connection connection, int id, String tieba) throws SQLException {
        PreparedStatement pstm = null;
        int excute = 0;
        if (connection != null) {
            String sql1 = "delete from post where post_id = ?";
            Object[] params1 = {id};
            excute = BaseDao.excute(connection, pstm, sql1, params1);
            if (excute > 0) {
                String sql2 = "update tieba set post_num=post_num-1 where name=?";
                Object[] params2 = {tieba};
                BaseDao.excute(connection, pstm, sql2, params2);
            }
            BaseDao.closeResource(null, pstm, null);
        }
        return excute;
    }

    //获取贴吧下的所有帖子
    @Override
    public List<Post> getPostList(Connection connection, String tieba) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Post> posts = new ArrayList<>();
        String sql = "select * from post where tieba = ?";
        Object[] params = {tieba};
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            Post post = new Post();
            post.setPostId(rs.getInt("post_id"));
            post.setTitle(rs.getString("title"));
            post.setTieba(rs.getString("tieba"));
            post.setTime(rs.getString("time"));
            post.setContent(rs.getString("content"));
            post.setWriter(rs.getString("writer"));
            post.setComment(rs.getInt("comment"));
            post.setThumb(rs.getInt("thumb"));
            posts.add(post);
        }
        BaseDao.closeResource(null, pstm, rs);
        return posts;
    }

    //获取收藏、点赞的帖子
    @Override
    public Post getPost(Connection connection, int postId) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Post post = new Post();
        Object[] params = {postId};
        String sql = "select * from post where post_id = ?";
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        if (rs.next()) {
            post.setPostId(rs.getInt("post_id"));
            post.setTitle(rs.getString("title"));
            post.setTieba(rs.getString("tieba"));
            post.setTime(rs.getString("time"));
            post.setContent(rs.getString("content"));
            post.setWriter(rs.getString("writer"));
            post.setComment(rs.getInt("comment"));
            post.setThumb(rs.getInt("thumb"));
        }
        BaseDao.closeResource(null, pstm, rs);
        return post;
    }
}
